import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken(), // 从缓存中读取初始值
  userInfo: ''
}
const mutations = {
  setToken(state, token) {
    state.token = token
    console.log(state.token)
    // 同步到缓存
    setToken(token)
  },
  removeToken(state) {
    // 删除Vuex的token
    state.token = null
    removeToken()
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  }
}
const actions = {
  // context上下文，传入参数
  async login(context, data) {
    const res = await login(data)
    const token = res.token
    const info = res.userName
    console.log(info)
    context.commit('setUserInfo', info)
    context.commit('setToken', token)
  },
  logout(context) {
    // 删除 token
    // 删除用户信息
    context.commit('removeToken')
    context.commit('setUserInfo', {})
  },
  getInfo() {}
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
